﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Lib
{
    public class ImageProcess
    {
        public static Bitmap ImageToBlackWhite(Bitmap bitmap)
        {
            Bitmap bmp = (Bitmap)bitmap.Clone();
            int rgb;
            Color c;

            for (int y = 0; y < bmp.Height; y++)
                for (int x = 0; x < bmp.Width; x++)
                {
                    c = bmp.GetPixel(x, y);
                    rgb = (int)((c.R + c.G + c.B) / 3);
                    bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
                }
            return bmp;

        }
        public static byte[] ConvertImageToByte(Image hinh)
        {
            System.IO.MemoryStream vungnho = new System.IO.MemoryStream();
            hinh.Save(vungnho, System.Drawing.Imaging.ImageFormat.Tiff);
            return vungnho.ToArray();
        }
        public static Image ConvertByteToImage(byte[] dulieu)
        {
            System.IO.MemoryStream vungnho = new System.IO.MemoryStream(dulieu);
            Image hinh = Image.FromStream(vungnho);
            return hinh;
        }
    }
}
